package com.xxxcompany.world.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xxxcompany.world.entity.Country;
import com.xxxcompany.world.service.ICountryService;
import com.xxxcompany.world.service.impl.CountryServiceImpl;

@WebServlet("/CountryServlet2")
public class CountryServlet2 extends HttpServlet 
{
	ICountryService cService = new CountryServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		String code = req.getParameter("code");
		
		// 输出JSON格式的数据
		List<Country> list = cService.findAll();
		if(code!=null)
		{
			for(Country c:list)
			{
				if(c.getCode().equals(code))
				{
					pw.print(JSON.toJSONString(c));
					break;
				}
				
			}
		}
		else
		{
			// 转换为json字符串
			pw.print(JSON.toJSONString(list));
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}